#include <stdio.h>
#include <malloc.h>

typedef struct Node{
    int data;
    struct Node *next;
} Node;

void printList(Node *head){
    if(head->next == NULL) {
        printf("NULL");
        return;
    }
    Node *temp = head->next;
    while (temp != NULL){
        printf("%d%s",temp->data,temp->next != NULL?" ":"");
        temp = temp->next;
    }
    return;
}

void input(Node *head){
    Node *t;
    int a;
    while (scanf("%d",&a)&&  a >= 0){
        t = (Node*)malloc(sizeof(Node));
        t->data = a;
        t->next = NULL;
        head->next = t;
        head = t;
    }
}

Node* intersection(Node *s1,Node *s2){
    Node *head = (Node*)malloc(sizeof(Node));
    head->next = NULL;
    Node *s3 = head;
    s1 = s1->next;
    s2 = s2->next;
    while(s1&&s2){
        if(s1->data==s2->data){
            Node *newNode = (Node *) malloc(sizeof(Node));
            newNode->data = s1->data;
            newNode->next = NULL;
            s3->next = newNode;
            s1 = s1->next;
            s2 = s2->next;
            s3 = s3->next;
        }else if(s1->data>s2->data){
            s2 = s2->next;
        } else{
            s1 = s1->next;
        }
    }
    return head;
}

int main(){
    Node *s1 = (Node *)malloc(sizeof (Node));
    Node *s2 = (Node *)malloc(sizeof (Node));
    s1->next = NULL;
    s2->next = NULL;
    input(s1);
    input(s2);
    Node *s3 = intersection(s1, s2);
    printList(s3);
    free(s1);
    free(s2);
    free(s3);
    return 0; // 退出程序
}